<template>
  <div class="pictureList-container">
    <van-tabs animated
              @click="getPicture">
      <van-tab v-for="(item, index) in tabsList"
               :key="index"
               :title="item.title"
               :name="item.type">
        <router-link v-for="(item, index) in pictureList"
                     :key="index"
                     :to="'pictureList/pictureInfo/'+item._id"
                     tag="div"
                     class="pictureItem">
          <img v-lazy="item.imgs[0]">
          <div class="desc">
            <h2>{{item.title}}</h2>
            <p>{{item.summary}}</p>
          </div>

        </router-link>
        <div class="getMore"
             v-if="pictureList.length">
          <van-button type="primary"
                      size="large"
                      @click="getMore(pictureList[0].type)"
                      round>加载更多</van-button>
        </div>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import { Toast } from 'vant'

export default {
  data() {
    return {
      pageNum: 1,
      pageSize: 5,
      tabsList: [],
      pictureList: [],
      type: 0
    }
  },
  created() {
    this.getTabs()
    this.getPictureByType(this.type)
  },
  methods: {
    // 获取导航信息
    getTabs() {
      this.axios.get('/tabs/getTabs').then(res => {
        if (res.data.code == 1) {
          this.tabsList = res.data.tabsList
        }
      })
    },
    // 获取图片
    getPictureByType(pageNum = 1, pageSize = 5, type = 0) {
      let params = { pageNum, pageSize, type }
      this.axios.get('/picture/getPictureByType', { params }).then(res => {
        if (res.data.code == 1) {
          if (res.data.pictureList.length == 0) {
            Toast(res.data.msg)
          } else {
            this.pictureList = this.pictureList.concat(res.data.pictureList)
          }
        }
      })
    },
    // 获取更多
    getMore(type) {
      this.pageNum++
      this.getPictureByType(this.pageNum, this.pageSize, this.type)
    },
    // 点击tabs，请求不同类型的图片
    getPicture(name, title) {
      this.pictureList = []
      this.type = name
      this.pageNum = 1
      this.getPictureByType(this.pageNum, this.pageSize, name)
    }
  }
}
</script>

<style lang="scss">
.pictureList-container {
  .pictureItem {
    width: 92vw;
    height: 6rem;
    margin: 0.3rem auto;
    position: relative;
    img {
      width: 100%;
      height: 100%;
      border-radius: 0.15rem;
    }
    .desc {
      position: absolute;
      width: 100%;
      left: 0;
      bottom: 0;
      color: #fff;
      background-color: rgba(0, 0, 0, 0.4);
      padding: 0.1rem 0.25rem;
      box-sizing: border-box;
      border-radius: 0 0 0.15rem 0.15rem;
      h2 {
        margin: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      p {
        margin: 0;
        line-height: 0.5rem;
      }
    }
  }
  .getMore {
    width: 92vw;
    margin: 0.2rem auto;
  }
}
</style>